﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TodoApi.Models;
using OrderManagement;
using Microsoft.EntityFrameworkCore;

namespace TodoApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly OrderContext orderDB;

        public OrderController(OrderContext context)
        {
            this.orderDB = context;
        }

        //   api/Order/id 为路径参数 查询ID=id的订单
        [HttpGet("{id}")]
        public ActionResult<Order> GetOrder(int id)
        {
            string ID = Convert.ToString(id);
            var order = orderDB.Orders.FirstOrDefault(order => order.ID == ID);
            if (order == null)
            {
                return NotFound();
            }
            return order;
        }

        // api/Order 为路径参数 查询所有订单
        [HttpGet]
        public ActionResult<List<Order>> GetOrderDetail()
        {
            //var query = buildQuery(name);
            //return query.ToList();
            IQueryable<Order> query = orderDB.Orders;
            return query.ToList();

        }

        //分页查询 api/Order/pageQuery?skip=x&&take=y 
        [HttpGet("pageQuery")]
        public ActionResult<List<Order>> queryTodoItem(string name, int skip, int take)
        {
            IQueryable<Order> query = orderDB.Orders.Include(o => o.Customer);
            query = query.Skip(skip).Take(take);
            return query.ToList();
        }

        // POST: api/Order
        [HttpPost]
        public ActionResult<Order> PostTodoItem(Order todo)
        {
            try
            {
                orderDB.Orders.Add(todo);
                orderDB.SaveChanges();
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }
            return todo;
        }

        // PUT: api/Order/{id}
        [HttpPut("{id}")]
        public ActionResult<Order> PutTodoItem(int id, Order order)
        {   String ID = Convert.ToString(id);
            
            if (ID != order.ID)
            {
                return BadRequest("Id cannot be modified!");
            }
            try
            {
                orderDB.Entry(order).State = EntityState.Modified;
                orderDB.SaveChanges();
            }
            catch (Exception e)
            {
                string error = e.Message;
                if (e.InnerException != null) error = e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }

        // DELETE: api/Order/{id}
        [HttpDelete("{id}")]
        public ActionResult DeleteTodoItem(int id)
        {
            try
            {
                String ID = Convert.ToString(id);
                var todo = orderDB.Orders.FirstOrDefault(order => order.ID == ID);
                if (todo != null)
                {
                    orderDB.Remove(todo);
                    orderDB.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }
            return NoContent();
        }

    }
}
